package com.mybase.app.fourBase;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * Created by happy_000 on 2014/12/10.
 *
 *
 */
public class BroadCastActivity extends Activity {
    public static final String ACTION_INTENT_TEST = "com.terry.broadcast.test";
    private MyBroadCastReceiver mBroadcastReceiver;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button btn =new Button(this);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ACTION_INTENT_TEST);
                sendBroadcast(intent);//发送广播
            }
        });
        setContentView(btn);
       // registerBoradcastReceiver();//动态注册广播
    }

    public void registerBoradcastReceiver(){
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTION_INTENT_TEST);
        //注册广播
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }
}
/***
 *  把广播接收器注册到系统里面，可以让系统知道我们有个广播接收器。这里有两种，
 一种是代码动态注册：
 //生成广播处理
 smsBroadCastReceiver = new SmsBroadCastReceiver();
 //实例化过滤器并设置要过滤的广播
 IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
 //注册广播
 BroadCastReceiverActivity.this.registerReceiver(smsBroadCastReceiver, intentFilter);
 一种是在AndroidManifest.xml中配置广播
 <receiver android:name=".SmsBroadCastReceiver">
 <intent-filter android:priority="20">
 <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
 </intent-filter>
 </receiver>
 */
